package com.zjl.paike2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zjl.paike2.entity.TeacherInfo;
import com.zjl.paike2.mapper.TeacherInfoMapper;
import com.zjl.paike2.service.TeacherInfoService;
import com.zjl.paike2.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

/**
 * 教师信息Service实现类
 */
@Service
public class TeacherInfoServiceImpl extends ServiceImpl<TeacherInfoMapper, TeacherInfo> implements TeacherInfoService {

    @Autowired
    private TeacherInfoMapper teacherInfoMapper;

    @Override
    public PageResult<TeacherInfo> getTeacherList(int pageNum, int pageSize) {
        // 使用PageHelper进行分页
        Page<TeacherInfo> page = PageHelper.startPage(pageNum, pageSize);
        
        // 查询教师列表（关联学院表）
        List<TeacherInfo> teacherList = teacherInfoMapper.selectTeacherList();
        
        // 返回分页结果
        return new PageResult<>(page.getTotal(), teacherList);
    }

    @Override
    public TeacherInfo getTeacherByCode(String teacherCode) {
        return teacherInfoMapper.selectTeacherByCode(teacherCode);
    }

    @Override
    public boolean addTeacher(TeacherInfo teacherInfo) {
        // 检查教师编号是否已存在
        TeacherInfo existTeacher = getById(teacherInfo.getTeacherCode());
        if (existTeacher != null) {
            return false;
        }
        
        // 设置默认密码，并进行MD5加密
        String password = teacherInfo.getPd();
        if (password == null || password.isEmpty()) {
            password = "123"; // 默认密码
        }
        // 进行密码加密后再保存
        teacherInfo.setPd(encryptPassword(password));
        
        return save(teacherInfo);
    }

    @Override
    public boolean updateTeacher(String teacherCode, TeacherInfo teacherInfo) {
        // 检查教师是否存在
        TeacherInfo existTeacher = getById(teacherCode);
        if (existTeacher == null) {
            return false;
        }
        
        // 设置教师编号
        teacherInfo.setTeacherCode(teacherCode);
        
        // 只更新非空字段
        if (teacherInfo.getTeacherName() == null) {
            teacherInfo.setTeacherName(existTeacher.getTeacherName());
        }
        if (teacherInfo.getTeacherTitle() == null) {
            teacherInfo.setTeacherTitle(existTeacher.getTeacherTitle());
        }
        if (teacherInfo.getSex() == null) {
            teacherInfo.setSex(existTeacher.getSex());
        }
        if (teacherInfo.getCollegeNo() == null) {
            teacherInfo.setCollegeNo(existTeacher.getCollegeNo());
        }
        // 保持密码不变
        teacherInfo.setPd(existTeacher.getPd());
        
        return updateById(teacherInfo);
    }

    @Override
    public boolean deleteTeacher(String teacherCode) {
        // 检查教师是否存在
        TeacherInfo existTeacher = getById(teacherCode);
        if (existTeacher == null) {
            return false;
        }
        
        return removeById(teacherCode);
    }
    
    /**
     * 密码加密处理
     * @param password 原始密码
     * @return 加密后的密码
     */
    private String encryptPassword(String password) {
        // 使用MD5对密码进行加密
        return DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
    }
} 